using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading;
namespace MicroMsgBackground.Common.Utils
{
	public static class Log
	{
		private const string TAG = "Log";
		public const int LEVEL_VERBOSE = 0;
		public const int LEVEL_DEBUG = 1;
		public const int LEVEL_INFO = 2;
		public const int LEVEL_WARNING = 3;
		public const int LEVEL_ERROR = 4;
		public const int LEVEL_NONE = 5;
		private const int STRING_LOG_BUFFER_SIZE = 524288;
		private const string LOG_FILENAME = "/background/MicroMsgBackground.log";
		private const string BAK_FILENAME = "/background/MicroMsgBackground.bak";
		private const int INTERVAL_CHECK_LOG = 30000;
		private static int level = 1;
		private static bool writeToFile = true;
		public static bool sBIsWriteToLogView = true;
		public static int MAX_FILE_LEN = 3145728;
		private static List<string> logCache = new List<string>();
		public static int Level
		{
			set
			{
				Log.level = value;
				Log.print(" -a ", "Log", "--------------new log begin, level: " + value + "------------------\n");
			}
		}
		public static void smethod_0(string TAG, object object_0)
		{
		}
		public static void smethod_1(string TAG, object object_0)
		{
			if (Log.level <= 4)
			{
				Log.print(" -e ", TAG, object_0);
			}
		}
		public static void smethod_2(string TAG, object object_0, Exception exception_0)
		{
			if (Log.level <= 4)
			{
				Log.print(" -e ", TAG, object_0 + ((exception_0 != null) ? (exception_0.GetType().get_FullName() + ":" + exception_0.get_Message()) : ""));
			}
		}
		public static void smethod_3(string TAG, object object_0)
		{
			if (Log.level <= 3)
			{
				Log.print(" -w ", TAG, object_0);
			}
		}
		public static void smethod_4(string TAG, object object_0)
		{
			if (Log.level <= 2)
			{
				Log.print(" -i ", TAG, object_0);
			}
		}
		public static void smethod_5(string TAG, object object_0)
		{
			if (Log.level <= 1)
			{
				Log.print(" -d ", TAG, object_0);
			}
		}
		public static void smethod_6(string TAG, object object_0)
		{
			if (Log.level <= 0)
			{
				Log.print(" -v ", TAG, object_0);
			}
		}
		public static string[] strSplit(string string_0, int sub_len)
		{
			int num = string_0.get_Length() / sub_len;
			int num2 = string_0.get_Length() % sub_len;
			if (num2 != 0)
			{
				num++;
			}
			string[] array = new string[num];
			for (int i = 0; i < num; i++)
			{
				if (num2 != 0 && i == num - 1)
				{
					array[i] = string_0.Substring(i * sub_len, num2);
					return array;
				}
				array[i] = string_0.Substring(i * sub_len, sub_len);
			}
			return array;
		}
		private static void print(string level, string context, object object_0)
		{
			string text = string.Concat(new object[]
			{
				DateTime.get_Now().ToString("dd-HH:mm:ss:fff "),
				Thread.get_CurrentThread().get_ManagedThreadId().ToString(),
				level,
				"/",
				context.PadRight(15).Substring(0, 15),
				": ",
				object_0
			});
			text += "\n";
			if (!Log.writeToFile)
			{
				return;
			}
			lock (Log.logCache)
			{
				Log.logCache.Add(text);
			}
		}
		public static void LogToFile()
		{
			if (Log.writeToFile && Log.logCache.get_Count() > 0)
			{
				string text = "";
				try
				{
					IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
					userStoreForApplication.CreateDirectory("background");
					IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("/background/MicroMsgBackground.log", 6, 2, 3, userStoreForApplication);
					StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream);
					if (streamWriter != null)
					{
						lock (Log.logCache)
						{
							for (int i = 0; i < Log.logCache.get_Count(); i++)
							{
								text += Log.logCache.get_Item(i);
							}
							Log.logCache.Clear();
						}
						if (isolatedStorageFileStream.get_Length() > (long)Log.MAX_FILE_LEN)
						{
							streamWriter.Close();
							isolatedStorageFileStream.Close();
							if (userStoreForApplication.FileExists("/background/MicroMsgBackground.bak"))
							{
								userStoreForApplication.DeleteFile("/background/MicroMsgBackground.bak");
							}
							userStoreForApplication.MoveFile("/background/MicroMsgBackground.log", "/background/MicroMsgBackground.bak");
							isolatedStorageFileStream = new IsolatedStorageFileStream("/background/MicroMsgBackground.log", 6, userStoreForApplication);
							streamWriter = new StreamWriter(isolatedStorageFileStream);
						}
						streamWriter.Write(text);
						text = "";
						streamWriter.Close();
						isolatedStorageFileStream.Close();
					}
				}
				catch (Exception ex)
				{
					Console.get_Out().Write(ex.get_Message());
				}
				return;
			}
		}
		public static void testLog()
		{
			Log.smethod_0("class2", "this is message");
			Log.smethod_1("class2", "this is error");
			Exception exception_ = new Exception("test exception");
			Log.smethod_2("class2", "this is error with exception message", exception_);
			Log.smethod_3("class2", "this is warn");
			Log.smethod_4("class2", "this is info ");
			Log.smethod_5("class2", "this is debug");
			Log.smethod_6("class1", "this is verbose");
		}
	}
}
